﻿namespace Community.CsharpSqlite.Entity
{
    /*
  ** CAPI3REF: Mutex Methods Object
  ** EXPERIMENTAL
  **
  ** An instance of this structure defines the low-level routines
  ** used to allocate and use mutexes.
  **
  ** Usually, the default mutex implementations provided by SQLite are
  ** sufficient, however the user has the option of substituting a custom
  ** implementation for specialized deployments or systems for which SQLite
  ** does not provide a suitable implementation. In this case, the user
  ** creates and populates an instance of this structure to pass
  ** to ConfigHelper.Sqlite3Config() along with the [ConfigOption.SQLITE_CONFIG_MUTEX] option.
  ** Additionally, an instance of this structure can be used as an
  ** output variable when querying the system for the current mutex
  ** implementation, using the [ConfigOption.SQLITE_CONFIG_GETMUTEX] option.
  **
  ** ^The xMutexInit method defined by this structure is invoked as
  ** part of system initialization by the sqlite3_initialize() function.
  ** ^The xMutexInit routine is calle by SQLite exactly once for each
  ** effective call to [sqlite3_initialize()].
  **
  ** ^The xMutexEnd method defined by this structure is invoked as
  ** part of system shutdown by the sqlite3_shutdown() function. The
  ** implementation of this method is expected to release all outstanding
  ** resources obtained by the mutex methods implementation, especially
  ** those obtained by the xMutexInit method.  ^The xMutexEnd()
  ** interface is invoked exactly once for each call to [sqlite3_shutdown()].
  **
  ** ^(The remaining seven methods defined by this structure (xMutexAlloc,
  ** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
  ** xMutexNotheld) implement the following interfaces (respectively):
  **
  ** <ul>
  **   <li>  [MutexHelper.sqlite3_mutex_alloc()] </li>
  **   <li>  [MutexHelper.MutexFree()] </li>
  **   <li>  [MutexHelper.MutexEnter()] </li>
  **   <li>  [MutexHelper.MutexTry()] </li>
  **   <li>  [MutexHelper.MutexLeave()] </li>
  **   <li>  [MutexHelper.MutexHeld()] </li>
  **   <li>  [MutexHelper.MutexNotheld()] </li>
  ** </ul>)^
  **
  ** The only difference is that the public sqlite3_XXX functions enumerated
  ** above silently ignore any invocations that pass a NULL pointer instead
  ** of a valid mutex handle. The implementations of the methods defined
  ** by this structure are not required to handle this case, the results
  ** of passing a NULL pointer instead of a valid mutex handle are undefined
  ** (i.e. it is acceptable to provide an implementation that segfaults if
  ** it is passed a NULL pointer).
  **
  ** The xMutexInit() method must be threadsafe.  ^It must be harmless to
  ** invoke xMutexInit() mutiple times within the same process and without
  ** intervening calls to xMutexEnd().  Second and subsequent calls to
  ** xMutexInit() must be no-ops.
  **
  ** ^xMutexInit() must not use SQLite memory allocation ([Malloc.sqlite3_malloc()]
  ** and its associates).  ^Similarly, xMutexAlloc() must not use SQLite memory
  ** allocation for a static mutex.  ^However xMutexAlloc() may use SQLite
  ** memory allocation for a fast or recursive mutex.
  **
  ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
  ** called, but only if the prior call to xMutexInit returned StatusCode.SQLITE_OK.
  ** If xMutexInit fails in any way, it is expected to clean up after itself
  ** prior to returning.
  */
    //typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
    //struct sqlite3_mutex_methods {
    //  int (*xMutexInit)(void);
    //  int (*xMutexEnd)(void);
    //  sqlite3_mutex *(*xMutexAlloc)(int);
    //  void (*xMutexFree)(sqlite3_mutex *);
    //  void (*xMutexEnter)(sqlite3_mutex *);
    //  int (*xMutexTry)(sqlite3_mutex *);
    //  void (*xMutexLeave)(sqlite3_mutex *);
    //  int (*xMutexHeld)(sqlite3_mutex *);
    //  int (*xMutexNotheld)(sqlite3_mutex *);
    //};
    public class sqlite3_mutex_methods
    {
        public dxMutexAlloc xMutexAlloc;
        public dxMutexEnd xMutexEnd;
        public dxMutexEnter xMutexEnter;
        public dxMutexFree xMutexFree;
        public dxMutexHeld xMutexHeld;
        public dxMutexInit xMutexInit;
        public dxMutexLeave xMutexLeave;
        public dxMutexNotheld xMutexNotheld;
        public dxMutexTry xMutexTry;

        public sqlite3_mutex_methods(
            dxMutexInit xMutexInit,
            dxMutexEnd xMutexEnd,
            dxMutexAlloc xMutexAlloc,
            dxMutexFree xMutexFree,
            dxMutexEnter xMutexEnter,
            dxMutexTry xMutexTry,
            dxMutexLeave xMutexLeave,
            dxMutexHeld xMutexHeld,
            dxMutexNotheld xMutexNotheld
            )
        {
            this.xMutexInit = xMutexInit;
            this.xMutexEnd = xMutexEnd;
            this.xMutexAlloc = xMutexAlloc;
            this.xMutexFree = xMutexFree;
            this.xMutexEnter = xMutexEnter;
            this.xMutexTry = xMutexTry;
            this.xMutexLeave = xMutexLeave;
            this.xMutexHeld = xMutexHeld;
            this.xMutexNotheld = xMutexNotheld;
        }
    };
}